package com.example.controller;

import com.example.anno.Log;
import com.example.pojo.Emp;
import com.example.pojo.Result;
import com.example.pojo.pageBean;
import com.example.service.impl.EmpServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;

//@RestController=@Controller+@ResponseBody
@RestController
@Slf4j
@RequestMapping("/emps")
public class EmpController {

    @Autowired
    private EmpServiceImpl empService;
    @GetMapping
    public Result list(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       @DateTimeFormat(pattern = "yyyy-MM-dd" ) LocalDate begin,
                       @DateTimeFormat(pattern = "yyyy-MM-dd" ) LocalDate end,
                       String name,Short gender){
        log.info("动态查询员工,参数:{},{},{},{},{},{}",name,gender,begin,end,page,pageSize);
        pageBean list= empService.list(page,pageSize,name,gender,begin,end);
        return Result.succeed(list);
    }
    @DeleteMapping("/{ids}")
    @Log
    public Result deleteById(@PathVariable int[] ids){
        empService.deleteById(ids);
        return Result.succeed();
    }
    @PostMapping
    @Log
    public Result add(@RequestBody Emp emp){
        empService.add(emp);
        return Result.succeed();

    }
    @GetMapping("/{id}")
    public Result getEmpById(@PathVariable Integer id){
        Emp emp=empService.getEmpById(id);
        return Result.succeed(emp);
    }
    @PutMapping
    @Log
    public Result updateById(@RequestBody Emp emp){
        empService.updateById(emp);
        return Result.succeed();
    }
}
